package com.example.hw1.controller;



import com.example.hw1.entity.Course;
import com.example.hw1.entity.Student;
import jdk.nashorn.internal.runtime.ErrorManager;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("/Student")

public class DesignController {
    private Student s;
    private List<Course> checkList;

    @GetMapping("/addStudent")
    public String addStudent(Model model){
        //跳转到学生个人信息页面
        Student s=new Student();
        model.addAttribute("Student",s);

        return "Student";


    }


    @GetMapping("/Course")
    public String selectCourse(@Valid @ModelAttribute("Student") Student student, Errors errors, Model model){

        if(errors.hasErrors()){
            return "Student";
        }
        //实例化学生对象
        s=new Student();
        s=student;


        //添加课程
         List<Course> courses=Arrays.asList(
                 new Course("高等数学"),
                 new Course("高等代数"),
                 new Course("离散数学"),
                 new Course("数据库"),
                 new Course("软件工程"),
                 new Course("计算机组成原理"),
                 new Course("JavaWeb"),
                 new Course("Python"),
                 new Course("C++"),
                 new Course("篮球"),
                 new Course("足球"),
                 new Course("乒乓球"),
                 new Course("两性心理"),
                 new Course( "手工艺"),
                 new Course("LOL打野科目二")
         );

        model.addAttribute("Student",s);
        model.addAttribute("courses",courses);

        return "Course";
    }

    @PostMapping("/Course")
    public String showResult(Model model,HttpServletRequest request){
        checkList=new ArrayList<>();
        String[] checkCourses=request.getParameterValues("checkCourses");
        for(int i=0;i<checkCourses.length;i++)
            checkList.add(new Course(checkCourses[i]));

        s.setCourseList(checkList);



        model.addAttribute("Student",s);
        model.addAttribute("courses",checkList);
        return "Result";
    }
}
